package gen

import java.nio.file.Files
import java.nio.file.Paths
import java.time.LocalDateTime
import java.util.*
import java.util.stream.Collectors
import java.util.stream.IntStream

class Generator {

    var item1 = {

        """ db.user.insertOne({
            | resourceId: '${ randomString(16) }',
            | type: '电脑',
            | kind: '${ randomValue(arrayOf("品牌A", "品牌B", "品牌C", "品牌D")) }',
            | price: ${ randomDouble(low = 3000, high = 5000) },
            | level: '${ randomValue(arrayOf("高配", "中配", "低配", "服务器")) }'
            | })
        """.trimMargin()
    }

    var item2 = {

        """ db.user.insertOne({
            | resourceId: '${ randomString(16) }',
            | type: '耗材',
            | kind: '${ randomValue(arrayOf("耗材A", "耗材B", "耗材C", "耗材D")) }',
            | price: ${ randomDouble(low = 100, high = 500) },
            | color: '${ randomValue(arrayOf("红色", "蓝色", "绿色", "白色", "黑色")) }',
            | isAccessory: ${ randomBoolean() }
            | })
        """.trimMargin()
    }

    var item3 = {

        """ db.user.insertOne({
            | resourceId: '${ randomString(16) }',
            | type: '食材',
            | kind: '${ randomValue(arrayOf("食材A", "食材B", "食材C", "食材D")) }',
            | price: ${ randomDouble(low = 5, high = 50) },
            | production: ISODate('${ randomDateBefore(offset = 24) }'),
            | quality: ISODate('${ randomDateAfter(offset = 24) }'),
            | flavor: '${ randomValue(arrayOf("甜", "咸", "辣", "淡")) }'
            | })
        """.trimMargin()
    }

    companion object {

        val random = Random()

        val charset = "zxcvbnmasdfghjklqwertyuiop1234567890"

        fun randomInt(): Int {

            return random.nextInt(Int.MAX_VALUE)
        }

        fun randomString(length: Int): String {

            return IntStream.rangeClosed(1, length).mapToObj { charset[random.nextInt(charset.length)].toString() }.collect(Collectors.joining())
        }

        fun randomBoolean(): Boolean {

            return random.nextBoolean()
        }

        fun randomDouble(low: Int = 0, high: Int = 5000): Double {

            return random.nextInt(high - low).toDouble() + low
        }

        fun <T> randomValue(set: Array<T>): T {

            return set[randomInt() % set.size]
        }

        fun randomDateBefore(point: LocalDateTime = LocalDateTime.now(), offset: Int = 24): LocalDateTime {

            return point.minusHours(random.nextInt(offset).toLong())
        }

        fun randomDateAfter(point: LocalDateTime = LocalDateTime.now(), offset: Int = 24): LocalDateTime {

            return point.plusHours(random.nextInt(offset).toLong())
        }
    }
}

fun main(args: Array<String>) {

    val g = Generator()

    val sb = StringBuilder()

    for(i in 0 .. 100000) {

        when(Generator.randomValue(arrayOf(1,2,3))) {

            1 -> sb.append(g.item1.invoke()).append("\n")

            2 -> sb.append(g.item2.invoke()).append("\n")

            3 -> sb.append(g.item3.invoke()).append("\n")
        }
    }

    Files.write(Paths.get("E:\\mongodb-test-data.txt"), sb.toString().toByteArray())
}



